package com.mobilesales.android.activities.tasks;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.mobilesales.android.R;
import com.mobilesales.android.activities.ListModelActivity;
import com.mobilesales.android.adapters.TaskArrayAdapter;
import com.mobilesales.android.model.tasks.CyclicTask;
import com.mobilesales.android.model.tasks.Task;
import com.mobilesales.android.util.Constants;
import com.mobilesales.android.util.DateUtil;
import com.mobilesales.android.util.MessageBox;
import com.mobilesales.android.util.Temp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public class ListTask extends ListModelActivity<Task> {

	private TextView currentDateField;

	private List<Task> tasksOfDay;
	private Date selectedDate;
	private TaskArrayAdapter itemsAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.task_list);
		currentDateField = (TextView) this.findViewById(R.id.currentDate);

		Bundle bundle = this.getIntent().getExtras();
		if (bundle != null && bundle.containsKey(Constants.SelectedDate)) {
			selectedDate = (Date) bundle.get(Constants.SelectedDate);
			String currentDateStr = String.format("%s: %s",
					getString(R.string.CurrentDay),
					DateUtil.toShortDateString(selectedDate));
			currentDateField.setText(currentDateStr);
			loadItems();
		} else {
			MessageBox.show(this, getString(R.string.ErrorTaskList));
			finish();
		}
	}

	private void loadItems() {
		tasksOfDay = this.getItems();
		if (tasksOfDay != null) {
			this.setItemsSource(tasksOfDay);
		}
		else {
			MessageBox.show(this, getString(R.string.ErrorTaskList));
			finish();
		}
	}

	@Override
	protected void onListItemClick(ListView listView, View view, int position,
			long id) {
		super.onListItemClick(listView, view, position, id);
		Task task = itemsAdapter.getItem(position);
		boolean isCyclicTask = (task instanceof CyclicTask);
		Intent intent = new Intent(this, DetailsTask.class);
		intent.putExtra(Constants.TaskId, task.Id);
		intent.putExtra(Constants.IsCyclicTask, isCyclicTask);
		this.startActivityForResult(intent, 0);
	}

	private CyclicTask[] getCyclicTasks(Date date) {
		// TODO get cyclic tasks from local database
		return Temp.getCyclicTasks();
	}

	private Task[] getTasks(Date date) {
		// TODO get tasks from local database
		return Temp.getTasks();
	}

	public void addTask(View v) {
		Intent intent = new Intent(this, StoreTask.class);
		intent.putExtra(Constants.IsCyclicTask, false);
		intent.putExtra(Constants.SelectedDate, this.selectedDate);
		this.startActivityForResult(intent, 0);
	}
	
	public void addCyclicTask(View v) {
		Intent intent = new Intent(this, StoreTask.class);
		intent.putExtra(Constants.IsCyclicTask, true);
		intent.putExtra(Constants.SelectedDate, this.selectedDate);
		this.startActivityForResult(intent, 0);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		this.loadItems();
	}

	public void finish(View v) {
		finish();
	}

	public void refresh() {
		this.loadItems();
	}

	@Override
	protected void setItemsSource(List<Task> source) {
		itemsAdapter = new TaskArrayAdapter(this, R.layout.task_list_item,
				source);
		this.setListAdapter(itemsAdapter);
	}

	@Override
	protected List<Task> getItems() {
		if (this.selectedDate != null) {
			Task[] tasks = getTasks(selectedDate);
			CyclicTask[] cyclicTasks = getCyclicTasks(selectedDate);
			List<Task> tasksOfDay = new ArrayList<Task>();
			if (tasks != null && cyclicTasks != null) {
				for (Task tsk : tasks)
					tasksOfDay.add(tsk);
				for (CyclicTask tsk : cyclicTasks)
					tasksOfDay.add(tsk);
				return tasksOfDay;
			}
		}
		return null;
	}
}
